

#import "FAMWeightViewController.h"

@interface FAMWeightViewController ()<UIPickerViewDelegate,UIPickerViewDataSource>
@property (weak, nonatomic) IBOutlet UIView *FAMPageBackView;
@property (weak, nonatomic) IBOutlet UIButton *FAM_lbsButton;
@property (weak, nonatomic) IBOutlet UIButton *FAM_kgButton;

@property (nonatomic, strong)NSMutableArray *FAMLbsArray,*FAMKgArray;
@property (weak, nonatomic) IBOutlet UIPickerView *FAMPickerView;

@end

@implementation FAMWeightViewController
- (NSMutableArray *)FAMLbsArray{
    if (!_FAMLbsArray) {
        _FAMLbsArray = [NSMutableArray array];
        for (int i = 60; i<= 400; i++) {
            [_FAMLbsArray addObject:[NSString stringWithFormat:@"%d",i]];
        }
    }
    return _FAMLbsArray;
}
- (NSMutableArray *)FAMKgArray{
    if (!_FAMKgArray) {
        _FAMKgArray = [NSMutableArray array];
        for (int i = 30; i<= 200; i++) {
            [_FAMKgArray addObject:[NSString stringWithFormat:@"%d",i]];
        }
    }
    return _FAMKgArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.fd_prefersNavigationBarHidden = YES;
    [self FAM_showUi];
}
- (void)FAM_showUi{
    self.FAMPageBackView.layer.shadowColor = kCustom0xColor(0x000000, 1.0).CGColor;
    self.FAMPageBackView.layer.shadowOpacity = 0.15;
    self.FAMPageBackView.layer.shadowRadius = 1.5;
    self.FAMPageBackView.layer.shadowOffset = CGSizeMake(2, 2);
    self.FAMPickerView.delegate = self;
    self.FAMPickerView.dataSource = self;
    if (self.FAM_kgButton.isSelected) {
        [self.FAMPickerView selectRow:30 inComponent:0 animated:YES];
        [self.FAMFoodmateUserdataDictionary setValue:@"60" forKey:@"weight"];
    }else{
        float weight = 120 * lbs;
        [self.FAMFoodmateUserdataDictionary setValue:[NSString stringWithFormat:@"%0.2f",weight] forKey:@"weight"];
        [self.FAMPickerView selectRow:60 inComponent:0 animated:YES];
        
    }
}
- (IBAction)FAM_backButtonClick:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)FAM_lbsButtonClick:(UIButton *)sender {
    [sender setSelected:YES];
    sender.backgroundColor = kBlackColor;
    [sender setTitleColor:kWhiteColor forState:UIControlStateNormal];
    self.FAM_kgButton.backgroundColor = kClearColor;
    [self.FAM_kgButton setTitleColor:kBlackColor forState:UIControlStateNormal];
    [self.FAM_kgButton setSelected:NO];
    [self.FAMPickerView reloadAllComponents];
    [self.FAMPickerView selectRow:60 inComponent:0 animated:YES];
    float weight = 120 * lbs;
    self.FAMFoodmateUserdataDictionary[@"weight"] = [NSString stringWithFormat:@"%0.2f",weight];
}

- (IBAction)FAM_kgButtonClick:(UIButton *)sender {
    [sender setSelected:YES];
    sender.backgroundColor = kBlackColor;
    [sender setTitleColor:kWhiteColor forState:UIControlStateNormal];
    self.FAM_lbsButton.backgroundColor = kClearColor;
    [self.FAM_lbsButton setTitleColor:kBlackColor forState:UIControlStateNormal];
    [self.FAM_lbsButton setSelected:NO];
    [self.FAMPickerView reloadAllComponents];
    [self.FAMPickerView selectRow:30 inComponent:0 animated:YES];
    self.FAMFoodmateUserdataDictionary[@"weight"] = @"60";
}

- (IBAction)FAM_nextButtonClick:(UIButton *)sender {
    FAMGoalViewController *vc = [FAMGoalViewController new];
    vc.FAMFoodmateUserdataDictionary = self.FAMFoodmateUserdataDictionary;
    [self.navigationController pushViewController:vc animated:YES];
}


#pragma mark ---- UIPickerViewDelegate UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
    
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if (self.FAM_kgButton.isSelected) {
        return self.FAMKgArray.count;
    }else{
        return self.FAMLbsArray.count;
    }
    
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    if (self.FAM_kgButton.isSelected) {
        return [NSString stringWithFormat:@"%@kg",self.FAMKgArray[row]];
    }else{
        return [NSString stringWithFormat:@"%@lbs",self.FAMLbsArray[row]];
    }
    
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    if (self.FAM_kgButton.isSelected) {
        self.FAMFoodmateUserdataDictionary[@"weight"] = self.FAMKgArray[row];
    }else{
        NSInteger templbs = ((NSString *)self.FAMLbsArray[row]).integerValue;
        float weight = templbs * lbs;
        self.FAMFoodmateUserdataDictionary[@"weight"] = [NSString stringWithFormat:@"%0.2f",weight];
    }
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    return 60;
}
@end
